package com.auxgroup.hr.front.commons.utils;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Date;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;

import com.auxgroup.hr.front.commons.utils.DateFormatCheck.DateFormatCheckValidator;

/** 
 * @author lvliuhao 
 * @version 创建时间：2018年1月22日 下午2:21:04 
 * 日期格式校验自定义validator
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.FIELD})
@Constraint(validatedBy=DateFormatCheckValidator.class)
public @interface DateFormatCheck {
	String message() default "日期格式不正确";
    Class<?>[] groups() default { };
    Class<? extends Payload>[] payload() default { };
    String format() default "yyyy-MM-dd";
    
    class DateFormatCheckValidator implements ConstraintValidator<DateFormatCheck, String> {
    	String format = "";

    	@Override
    	public void initialize(DateFormatCheck constraintAnnotation) {
    		this.format = constraintAnnotation.format();
    	}

    	@Override
    	public boolean isValid(String value, ConstraintValidatorContext context) {
    		Date data =DateUtils.parseDate(value,format);
    		if(data==null){
    			return false;
    		}else{
    			return true;
    		}
    	}
    }
}
 